********************************************************************************
** Cederman, Galano, Girardin and Schvitz. War Did Make States.
** Article prepared for International Organization
** June 20, 2022
**
** Stata do-file: analysis_dyads.do
** Main script for dyadic analysis with Abramson data
** Required file paths set in runall.do
********************************************************************************


cd $ROOT
cd $INTERMEDIATEDIR
use dyad_analysis2, clear
 
gen peaceXgrowthwara = (1-inc1)*llgrowthwara
gen peaceXgrowthpeacea = (1-inc1)*llgrowthpeacea
gen peaceXshrinkwarb = (1-inc1)*llshrinkwarb
gen peaceXshrinkpeaceb = (1-inc1)*llshrinkpeaceb

gen lrwara = l5.growthwara/(l5.llareaa)
gen lrXlr = lr * lr

gen peaceXllr = (1-inc1)*llr
gen initXllr = incinita1 * llr

replace llelevsda = 0 if llelevsda == .
replace llelevsdb = 0 if llelevsdb == .


label variable lnga "log terr. gain A"
label variable lneigh "neighbors AB"
label variable llnumneighsa "\\# neighbors A"
label variable llnumneighsb "\\# neighbors B"
label variable lldist "log distance AB"
label variable inc1 "war AB"
label variable lr "rel. size A/AB"
label variable lrXlr "rel. size ^2"
label variable llr "rel. log size A/AB"
label variable inc1Xllr "war X rel. log size"
label variable peaceXllr "peace X rel. log size"
label variable llareaa "log size A"
label variable lcoastala "Coastal access A"
label variable lcoastalb "Coastal access B"
label variable llagea "log age A"
label variable llageb "log age B"
label variable llareab "log size B"
label variable llelevsda "log elev. SD A"
label variable llelevsdb "log elev SD B"

label variable lurbana "urban share A"
label variable lurbanb "urban share B"

label variable lcentrala "core (A)"
label variable lcentralb "core (B)"

label variable llgrowthwara "cumul. war gains A"
label variable llgrowthpeacea "cumul. peace gains A"
label variable inc1Xgrowthwara "war X war gains A"
label variable inc1Xgrowthpeacea "war X peace gains A"

label variable peaceXgrowthwara "peace X war gains A"
label variable peaceXgrowthpeacea "peace X peace gains A"


label variable llshrinkwarb "cumul. war losses B"
label variable llshrinkpeaceb "cumul. peace losses B"
label variable inc1Xshrinkwarb "war X war losses B"
label variable inc1Xshrinkpeaceb "war X peace losses B"

label variable peaceXshrinkwarb "peace X war losses B"
label variable peaceXshrinkpeaceb "peace X peace losses B"

label variable incinita1 "A initiator"
label variable initXllr "A init. X rel. size"

label variable llgrowtha "cumul. gains A"
label variable llshrinkb "cumul. losses B"


// GENERATE TABLE 3
// MAIN DYADIC MODELS FOR PAPER

eststo clear
global X "inc1Xgrowthwara peaceXgrowthwara inc1Xgrowthpeacea peaceXgrowthpeacea inc1Xshrinkwarb peaceXshrinkwarb inc1Xshrinkpeaceb peaceXshrinkpeaceb"
global X1 "inc1##c.llgrowthwara inc1##c.llgrowthpeacea inc1##c.llshrinkwarb inc1##c.llshrinkpeaceb"  // version for plots
global CONTROLS "lldist lneigh lcoastala lcoastalb llagea llageb llnumneighsa llnumneighsb  llelevsda llelevsdb"
global TIMEVARS "noeventyear evspline*"

reghdfe lnga0 inc1 llr  llareab $CONTROLS $TIMEVARS, absorb(year) cluster(idd2 ida idb)
estadd local geocontrols "Yes"
estadd local yearfe "Yes"
estadd local dyadfe "No"
estimates store m1
  
reghdfe lnga0 inc1 llr inc1Xllr   llareab $CONTROLS $TIMEVARS, absorb(year) cluster(idd2 ida idb)
estadd local geocontrols "Yes"
estadd local yearfe "Yes"
estadd local dyadfe "No"
estimates store m2

reghdfe lnga0 inc1 $X llareaa llareab $CONTROLS $TIMEVARS, absorb(year) cluster(idd2 ida idb)
estadd local geocontrols "Yes"
estadd local yearfe "Yes"
estadd local dyadfe "No"
estimates store m3

reghdfe lnga0 inc1 $X llareaa llareab $CONTROLS $TIMEVARS, absorb(year idd) cluster(idd2 ida idb)
estadd local geocontrols "Yes"
estadd local yearfe "Yes"
estadd local dyadfe "Yes"
estimates store m4

cd $ROOT
cd $OUTPUTDIR
estimates save "dyads.ster", replace

esttab m1 m2 m3 m4 using "dyads.tex", replace /// 
b(4) se label pr2 star(+ 0.1 * 0.05 ** 0.01 *** 0.001) ///
nogaps eqlabels(none) obslast nomtitles ///
title("Dyadic analysis of gains of State A and losses of State B \label{tab:dyadmain}") ///
order (inc1 llr inc1Xllr $X  llareaa llareab) /// $CONTROLS) ///
keep (inc1 llr inc1Xllr $X llareaa  llareab) /// $CONTROLS) ///
 nonotes addnotes("Standard errors clustered on states and dyads in parentheses." /// 
 "$^{+}p<0.1$; $^{*}p<0.05$; $^{**}p<0.01.$; $^{***}p<0.001.$ ") ///
      scalars("geocontrols Geo. Controls" "yearfe Year FE" "dyadfe Dyad FE")


// GENERATE TABLE A2
 // BINARY DV BASED ON MAIN DYADIC MODELS

eststo clear
global X "inc1Xgrowthwara peaceXgrowthwara inc1Xgrowthpeacea peaceXgrowthpeacea inc1Xshrinkwarb peaceXshrinkwarb inc1Xshrinkpeaceb peaceXshrinkpeaceb"
global X1 "inc1##c.llgrowthwara inc1##c.llgrowthpeacea inc1##c.llshrinkwarb inc1##c.llshrinkpeaceb"  // version for plots
global CONTROLS "lldist lneigh lcoastala lcoastalb llagea llageb llnumneighsa llnumneighsb  llelevsda llelevsdb"
global TIMEVARS "noeventyear evspline*"

reghdfe nga10 inc1 llr  llareab $CONTROLS $TIMEVARS, absorb(year) cluster(idd2 ida idb)
estadd local geocontrols "Yes"
estadd local yearfe "Yes"
estadd local dyadfe "No"
estimates store m1
  
reghdfe nga10 inc1 llr inc1Xllr  llareab $CONTROLS $TIMEVARS, absorb(year) cluster(idd2 ida idb)
estadd local geocontrols "Yes"
estadd local yearfe "Yes"
estadd local dyadfe "No"
estimates store m2

reghdfe nga10 inc1 $X llareaa llareab $CONTROLS $TIMEVARS, absorb(year) cluster(idd2 ida idb)
estadd local geocontrols "Yes"
estadd local yearfe "Yes"
estadd local dyadfe "No"
estimates store m3

reghdfe nga10 inc1 $X llareaa llareab $CONTROLS $TIMEVARS, absorb(year idd) cluster(idd2 ida idb)
estadd local geocontrols "Yes"
estadd local yearfe "Yes"
estadd local dyadfe "Yes"
estimates store m4

cd $ROOT
cd $OUTPUTDIR
estimates save "dyads_binary.ster", replace

esttab m1 m2 m3 m4 using "dyads_binary.tex", replace /// 
b(4) se label pr2 star(+ 0.1 * 0.05 ** 0.01 *** 0.001) ///
nogaps eqlabels(none) obslast nomtitles ///
title("Dyadic analysis of gains of State A and losses of State B (binary Dv) \label{tab:dyadbinary}") ///
order (inc1 llr inc1Xllr $X llareaa  llareab ) ///
keep (inc1 llr inc1Xllr $X llareaa  llareab ) ///
 nonotes addnotes("Standard errors clustered on states and dyads in parentheses." /// 
 "$^{+}p<0.1$; $^{*}p<0.05$; $^{**}p<0.01.$; $^{***}p<0.001.$ ") ///
      scalars("geocontrols Geo. Controls" "yearfe Year FE" "dyadfe Dyad FE")
	  

// GENERATE TABLE A3
// ROBUSTNESS ANALYSIS OF URBAN POPULATION AND PERIPHERAL POSITION IN EUROPE	    
eststo clear
global X "inc1Xgrowthwara peaceXgrowthwara inc1Xgrowthpeacea peaceXgrowthpeacea inc1Xshrinkwarb peaceXshrinkwarb inc1Xshrinkpeaceb peaceXshrinkpeaceb"
global X1 "inc1##c.llgrowthwara inc1##c.llgrowthpeacea inc1##c.llshrinkwarb inc1##c.llshrinkpeaceb"  // version for plots
global CONTROLS "lldist lneigh lcoastala lcoastalb llagea llageb llnumneighsa llnumneighsb  llelevsda llelevsdb"
global CONTROLS2 "lurbana lurbanb lcentrala lcentralb"
global TIMEVARS "noeventyear evspline*"

reghdfe lnga0 inc1 llr  llareab $CONTROLS $CONTROLS2 $TIMEVARS, absorb(year) cluster(idd2 ida idb)
estadd local geocontrols "Yes"
estadd local yearfe "Yes"
estadd local dyadfe "No"
estimates store m1
  
reghdfe lnga0 inc1 llr inc1Xllr   llareab $CONTROLS $CONTROLS2 $TIMEVARS, absorb(year) cluster(idd2 ida idb)
estadd local geocontrols "Yes"
estadd local yearfe "Yes"
estadd local dyadfe "No"
estimates store m2

reghdfe lnga0 inc1 $X llareaa llareab $CONTROLS $CONTROLS2 $TIMEVARS, absorb(year) cluster(idd2 ida idb)
estadd local geocontrols "Yes"
estadd local yearfe "Yes"
estadd local dyadfe "No"
estimates store m3

reghdfe lnga0 inc1 $X llareaa llareab $CONTROLS $CONTROLS2 $TIMEVARS, absorb(year idd) cluster(idd2 ida idb)
estadd local geocontrols "Yes"
estadd local yearfe "Yes"
estadd local dyadfe "Yes"
estimates store m4

cd $ROOT
cd $OUTPUTDIR
estimates save "dyadsurban.ster", replace


esttab m1 m2 m3 m4 using "dyadsurban.tex", replace /// 
b(4) se label pr2 star(+ 0.1 * 0.05 ** 0.01 *** 0.001) ///
nogaps eqlabels(none) obslast nomtitles ///
title("Dyadic analysis of gains of State A and losses of State B \label{tab:dyad-urb-centr}") ///
order (inc1 llr inc1Xllr $X llareaa  llareab $CONTROLS2) ///
keep (inc1 llr inc1Xllr $X llareaa  llareab $CONTROLS2) ///
 nonotes addnotes("Standard errors clustered on states and dyads in parentheses." /// 
 "$^{+}p<0.1$; $^{*}p<0.05$; $^{**}p<0.01.$; $^{***}p<0.001.$ ") ///
      scalars("geocontrols Geo. Controls" "yearfe Year FE" "dyadfe Dyad FE")	  
	  
	  
	  
// GENERATE TABLE A4	  
// DYADIC MODELS WITHOUT WAR VARIABLES

eststo clear
global CONTROLS "lldist lneigh lcoastala lcoastalb llagea llageb llnumneighsa llnumneighsb  llelevsda llelevsdb"
global TIMEVARS "noeventyear evspline*"

reghdfe lnga0  llr  llareab $CONTROLS $TIMEVARS, absorb(year) cluster(idd2 ida idb)
estadd local geocontrols "Yes"
estadd local yearfe "Yes"
estadd local dyadfe "No"
estimates store m1
  
reghdfe lnga0 inc1 llgrowtha llshrinkb llareaa llareab $CONTROLS $TIMEVARS, absorb(year) cluster(idd2 ida idb)
estadd local geocontrols "Yes"
estadd local yearfe "Yes"
estadd local dyadfe "No"
estimates store m2

reghdfe lnga0 inc1 llgrowtha llshrinkb llareaa llareab $CONTROLS $TIMEVARS, absorb(year idd) cluster(idd2 ida idb)
estadd local geocontrols "Yes"
estadd local yearfe "Yes"
estadd local dyadfe "Yes"
estimates store m3

cd $ROOT
cd $OUTPUTDIR
estimates save "dyad_nowar.ster", replace

esttab m1 m2 m3 using "dyads_nowar.tex", replace /// 
b(4) se label pr2 star(+ 0.1 * 0.05 ** 0.01 *** 0.001) ///
nogaps eqlabels(none) obslast nomtitles ///
title("Dyadic analysis of gains of State A and losses of State B without war variables \label{tab:dyad:nowar}") ///
order (llr   llareaa llareab llgrowtha llshrinkb $CONTROLS) ///
keep (llr   llareaa llareab llgrowtha llshrinkb $CONTROLS) ///
 nonotes addnotes("Standard errors clustered on states and dyads in parentheses." /// 
 "$^{+}p<0.1$; $^{*}p<0.05$; $^{**}p<0.01.$; $^{***}p<0.001.$ ") ///
      scalars("geocontrols Geo. Controls" "yearfe Year FE" "dyadfe Dyad FE")
	  

// GENERATE TABLE A6	  	  
// ROBUSTNESS TO CHANGES IN TECHNOLOGY (1650 cutoff)

eststo clear
global X "inc1Xgrowthwara peaceXgrowthwara inc1Xgrowthpeacea peaceXgrowthpeacea inc1Xshrinkwarb peaceXshrinkwarb inc1Xshrinkpeaceb peaceXshrinkpeaceb"
global X1 "inc1##c.llgrowthwara inc1##c.llgrowthpeacea inc1##c.llshrinkwarb inc1##c.llshrinkpeaceb"  // version for plots
global CONTROLS "lldist lneigh lcoastala lcoastalb llagea llageb llnumneighsa llnumneighsb  llelevsda llelevsdb"
global CONTROLS2 "lurbana lurbanb lcentrala lcentralb"
global TIMEVARS "noeventyear evspline*"

reghdfe lnga0 inc1 $X llareaa llareab $CONTROLS $TIMEVARS if year<1650, absorb(year) cluster(idd2 ida idb)
estadd local geocontrols "Yes"
estadd local yearfe "Yes"
estadd local dyadfe "No"
estimates store m1
  
reghdfe lnga0 inc1 $X llareaa llareab $CONTROLS $TIMEVARS if year>=1650, absorb(year) cluster(idd2 ida idb)
estadd local geocontrols "Yes"
estadd local yearfe "Yes"
estadd local dyadfe "No"
estimates store m2


cd $ROOT
cd $OUTPUTDIR
estimates save "dyads_milrevol.ster", replace

esttab m1 m2   using "dyads_milrevol.tex", replace /// 
b(4) se label pr2 star(+ 0.1 * 0.05 ** 0.01 *** 0.001) ///
nogaps eqlabels(none) obslast nomtitles ///
title("Dyadic analysis of gains and losses before and after 1650 \label{tab:milrevol}") ///
order (inc1 $X  llareaa llareab $CONTROLS) ///
keep (inc1 $X  llareaa llareab $CONTROLS) ///
 nonotes addnotes("Standard errors clustered on states and dyads in parentheses." /// 
 "$^{+}p<0.1$; $^{*}p<0.05$; $^{**}p<0.01.$; $^{***}p<0.001.$ ") ///
      scalars("geocontrols Geo. Controls" "yearfe Year FE" "dyadfe Dyad FE")	  
 
	  
	  
// GENERATE TABLE A7	  
// ROBUSTNESS TEST WITHOUT RUSSIA AND PRUSSIA

eststo clear
global X "inc1Xgrowthwara peaceXgrowthwara inc1Xgrowthpeacea peaceXgrowthpeacea inc1Xshrinkwarb peaceXshrinkwarb inc1Xshrinkpeaceb peaceXshrinkpeaceb"
global X1 "inc1##c.llgrowthwara inc1##c.llgrowthpeacea inc1##c.llshrinkwarb inc1##c.llshrinkpeaceb"  // version for plots
global CONTROLS "lldist lneigh lcoastala lcoastalb llagea llageb llnumneighsa llnumneighsb  llelevsda llelevsdb  "
global TIMEVARS "noeventyear evspline*"
global EXCLUDE "if namea!="Muscovy" & nameb!="Muscovy & namea!="Hohenzollern" & nameb!="Hohenzollern"

reghdfe lnga0 inc1 llr  llareab $CONTROLS $TIMEVARS $EXCLUDE, absorb(year) cluster(idd2 ida idb)
estadd local geocontrols "Yes"
estadd local yearfe "Yes"
estadd local dyadfe "No"
estimates store m1
  
reghdfe lnga0 inc1 llr inc1Xllr  llareab $CONTROLS $TIMEVARS $EXCLUDE, absorb(year) cluster(idd2 ida idb)
estadd local geocontrols "Yes"
estadd local yearfe "Yes"
estadd local dyadfe "No"
estimates store m2

reghdfe lnga0 inc1 $X llareaa llareab $CONTROLS $TIMEVARS $EXCLUDE, absorb(year) cluster(idd2 ida idb)
estadd local geocontrols "Yes"
estadd local yearfe "Yes"
estadd local dyadfe "No"
estimates store m3

reghdfe lnga0 inc1 $X llareaa llareab $CONTROLS $TIMEVARS $EXCLUDE, absorb(year idd) cluster(idd2 ida idb)
estadd local geocontrols "Yes"
estadd local yearfe "Yes"
estadd local dyadfe "Yes"
estimates store m4

cd $ROOT
cd $OUTPUTDIR
estimates save "dyads_noPRussia.tex.ster", replace

esttab m1 m2 m3 m4 using "dyads_noPRussia.tex", replace /// 
b(4) se label pr2 star(+ 0.1 * 0.05 ** 0.01 *** 0.001) ///
nogaps eqlabels(none) obslast nomtitles ///
title("Dyadic analysis of gains and losses without Russia and Prussia \label{tab:noPRussia}") ///
order (inc1 llr inc1Xllr $X  llareaa llareab) ///  $CONTROLS) ///
keep (inc1 llr inc1Xllr $X  llareaa llareab) /// $CONTROLS) ///
 nonotes addnotes("Standard errors clustered on states and dyads in parentheses." /// 
 "$^{+}p<0.1$; $^{*}p<0.05$; $^{**}p<0.01.$; $^{***}p<0.001.$ ") ///
      scalars("geocontrols Geo. Controls" "yearfe Year FE" "dyadfe Dyad FE")

	  
	  
// GENERATE TABLE A8	  
// ROBUSTNESS TEST WITHOUT EMPIRES

eststo clear
global X "inc1Xgrowthwara peaceXgrowthwara inc1Xgrowthpeacea peaceXgrowthpeacea inc1Xshrinkwarb peaceXshrinkwarb inc1Xshrinkpeaceb peaceXshrinkpeaceb"
global X1 "inc1##c.llgrowthwara inc1##c.llgrowthpeacea inc1##c.llshrinkwarb inc1##c.llshrinkpeaceb"  // version for plots
global CONTROLS "lldist lneigh lcoastala lcoastalb llagea llageb llnumneighsa llnumneighsb  llelevsda llelevsdb  "
global TIMEVARS "noeventyear evspline*"
global EXCLUDE "if namea!="Muscovy" & nameb!="Muscovy" & namea!="Habsburgs" & nameb!="Habsburgs" & namea!="Ottoman Emirate" & nameb!="Ottoman Emirate""

reghdfe lnga0 inc1 llr  llareab $CONTROLS $TIMEVARS $EXCLUDE, absorb(year) cluster(idd2 ida idb)
estadd local geocontrols "Yes"
estadd local yearfe "Yes"
estadd local dyadfe "No"
estimates store m1
  
reghdfe lnga0 inc1 llr inc1Xllr  llareab $CONTROLS $TIMEVARS $EXCLUDE, absorb(year) cluster(idd2 ida idb)
estadd local geocontrols "Yes"
estadd local yearfe "Yes"
estadd local dyadfe "No"
estimates store m2

reghdfe lnga0 inc1 $X llareaa llareab $CONTROLS $TIMEVARS $EXCLUDE, absorb(year) cluster(idd2 ida idb)
estadd local geocontrols "Yes"
estadd local yearfe "Yes"
estadd local dyadfe "No"
estimates store m3

reghdfe lnga0 inc1 $X llareaa llareab $CONTROLS $TIMEVARS $EXCLUDE, absorb(year idd) cluster(idd2 ida idb)
estadd local geocontrols "Yes"
estadd local yearfe "Yes"
estadd local dyadfe "Yes"
estimates store m4

cd $ROOT
cd $OUTPUTDIR
estimates save "dyads_noEmpire.tex.ster", replace

esttab m1 m2 m3 m4 using "dyads_noEmpire.tex", replace /// 
b(4) se label pr2 star(+ 0.1 * 0.05 ** 0.01 *** 0.001) ///
nogaps eqlabels(none) obslast nomtitles ///
title("Dyadic analysis of gains and losses without empires \label{tab:noEmpire}") ///
order (inc1 llr inc1Xllr $X  llareaa llareab $CONTROLS) ///
keep (inc1 llr inc1Xllr $X  llareaa llareab $CONTROLS) ///
 nonotes addnotes("Standard errors clustered on states and dyads in parentheses." /// 
 "$^{+}p<0.1$; $^{*}p<0.05$; $^{**}p<0.01.$; $^{***}p<0.001.$ ") ///
      scalars("geocontrols Geo. Controls" "yearfe Year FE" "dyadfe Dyad FE")
	  
	  


	  	  
// ROBUSTNESS TEST OVER THREE CENTURIES (NOT SHOWN IN PAPER OR APPENDIX)

eststo clear

reghdfe lnga0 inc1 $X llareaa llareab $CONTROLS $TIMEVARS if year<1590, absorb(year) cluster(idd2 ida idb)
estadd local geocontrols "Yes"
estadd local yearfe "Yes"
estadd local dyadfe "No"
estimates store m1
  
reghdfe lnga0 inc1 $X llareaa llareab $CONTROLS $TIMEVARS if year>=1590 & year<1690, absorb(year) cluster(idd2 ida idb)
estadd local geocontrols "Yes"
estadd local yearfe "Yes"
estadd local dyadfe "No"
estimates store m2

reghdfe lnga0 inc1 $X llareaa llareab $CONTROLS $TIMEVARS if year>=1690, absorb(year) cluster(idd2 ida idb)
estadd local geocontrols "Yes"
estadd local yearfe "Yes"
estadd local dyadfe "No"
estimates store m3

cd $ROOT
cd $OUTPUTDIR
estimates save "dyads_centuries.ster", replace

esttab m1 m2 m3  using "dyads_centuries.tex", replace /// 
b(4) se label pr2 star(+ 0.1 * 0.05 ** 0.01 *** 0.001) ///
nogaps eqlabels(none) obslast nomtitles ///
title("Dyadic analysis of gains and losses for centuries from 1490, 1590 and 1690 \label{tab:dyad_3cent}") ///
order (inc1 $X  llareaa llareab $CONTROLS) ///
keep (inc1 $X  llareaa llareab $CONTROLS) ///
 nonotes addnotes("Standard errors clustered on states and dyads in parentheses." /// 
 "$^{+}p<0.1$; $^{*}p<0.05$; $^{**}p<0.01.$; $^{***}p<0.001.$ ") ///
      scalars("geocontrols Geo. Controls" "yearfe Year FE" "dyadfe Dyad FE")
	  
	  

// GEMERATE TABLE A9
// MODELING DYADIC ONSET


eststo clear

global CUMULVARS "llgrowthwara llgrowthpeacea llshrinkwarb llshrinkpeaceb"
global CONTROLS "lldist lneigh lcoastala lcoastalb llagea llageb llnumneighsa llnumneighsb  llelevsda llelevsdb"
global TIMEVARS "pyis pyispline*"

gen onsetinita100 = onsetinita1*100
// lr lrXlr

logit onsetinita1  llr inc1Xllr llareab $CONTROLS $TIMEVARS i.year,  cluster(idd2)
estadd local estim "Logit"
estadd local geocontrols "Yes"
estadd local yearfe "Yes"
estadd local dyadfe "No"
estimates store m1

logit onsetinita1  $CUMULVARS llareaa llareab $CONTROLS $TIMEVARS i.year,  cluster(idd2)
estadd local estim "Logit"
estadd local geocontrols "Yes"
estadd local yearfe "Yes"
estadd local dyadfe "No"
estimates store m2

reghdfe onsetinita100 llr $CUMULVARS llareaa llareab $CONTROLS  $TIMEVARS, absorb(year) cluster(idd2 ida idb)
estadd local estim "OLS"
estadd local geocontrols "Yes"
estadd local yearfe "Yes"
estadd local dyadfe "No"
estimates store m3

reghdfe onsetinita100 llr $CUMULVARS llareaa llareab $CONTROLS  $TIMEVARS, absorb(year idd) cluster(idd2 ida idb)
estadd local estim "OLS"
estadd local geocontrols "Yes"
estadd local yearfe "Yes"
estadd local dyadfe "Yes"
estimates store m4

cd $ROOT
cd $OUTPUTDIR
estimates save "dyads_onset.ster", replace

esttab m1 m2 m3 m4 using "dyads_onset.tex", replace /// 
b(4) se label pr2 star(+ 0.1 * 0.05 ** 0.01 *** 0.001) ///
nogaps eqlabels(none) obslast nomtitles ///
title("Dyadic onset \label{tab:dyadonset}") ///
order (llr $CUMULVARS  llareaa llareab) ///
keep (llr $CUMULVARS  llareaa llareab) ///
 nonotes addnotes("Standard errors clustered on states and dyads in parentheses." /// 
 "$^{+}p<0.1$; $^{*}p<0.05$; $^{**}p<0.01.$; $^{***}p<0.001.$ ") ///
      scalars("estim Esimation" "geocontrols Geo. Controls" "yearfe Year FE" "dyadfe Dyad FE")
 
 


// GENERATE TABLE A10
// MODELING SECOND STAGE

eststo clear 

global X "inc1Xgrowthwara peaceXgrowthwara inc1Xgrowthpeacea peaceXgrowthpeacea inc1Xshrinkwarb peaceXshrinkwarb inc1Xshrinkpeaceb peaceXshrinkpeaceb"
global X1 "inc1##c.llgrowthwara inc1##c.llgrowthpeacea inc1##c.llshrinkwarb inc1##c.llshrinkpeaceb"  // version for plots
global CONTROLS "lldist lneigh lcoastala lcoastalb llagea llageb llnumneighsa llnumneighsb  llelevsda llelevsdb"
global TIMEVARS "noeventyear evspline*"


reghdfe lnga0 incinita1 llr initXllr llareab $CONTROLS noeventyear evspline* if inc1==1, absorb(year) cluster(idd2 ida idb)
estadd local sampledef "War"
estadd local geocontrols "Yes"
estadd local yearfe "Yes"
estadd local dyadfe "No"
estimates store m1

reghdfe lnga0  incinita1 llr initXllr inc1 llareab $CONTROLS $TIMEVARS, absorb(year) cluster(idd2 ida idb)
estadd local sampledef "All"
estadd local geocontrols "Yes"
estadd local yearfe "Yes"
estadd local dyadfe "No"
estimates store m2

reghdfe lnga0  incinita1 inc1 $X llareaa llareab $CONTROLS $TIMEVARS, absorb(year) cluster(idd2 ida idb)
estadd local sampledef "All"
estadd local geocontrols "Yes"
estadd local yearfe "Yes"
estadd local dyadfe "Yes"
estimates store m3

reghdfe lnga0  incinita1 inc1 $X llareaa llareab $CONTROLS $TIMEVARS, absorb(year idd) cluster(idd2 ida idb)
estadd local sampledef "All"
estadd local geocontrols "Yes"
estadd local yearfe "Yes"
estadd local dyadfe "Yes"
estimates store m4

cd $ROOT
cd $OUTPUTDIR
estimates save "dyads_gainsinwar.ster", replace

esttab m1 m2 m3 m4 using "dyads_gainsinwar.tex", replace /// 
b(4) se label pr2 star(+ 0.1 * 0.05 ** 0.01 *** 0.001) ///
nogaps eqlabels(none) obslast nomtitles ///
title("Dyadic gains \label{tab:dyad_2ndphase}") ///
order (inc1 incinita1 llr initXllr $X  llareaa llareab) /// $CONTROLS) ///
keep (inc1 incinita1 llr initXllr $X  llareaa llareab) /// $CONTROLS) ///
 nonotes addnotes("Standard errors clustered on states and dyads in parentheses." /// 
 "$^{+}p<0.1$; $^{*}p<0.05$; $^{**}p<0.01.$; $^{***}p<0.001.$ ") ///
      scalars("sampledef Sample" "geocontrols Geo. Controls" "yearfe Year FE" "dyadfe Dyad FE")
 
 
 
// INTERACTION PLOTS BASED ON MODELS 3 AND 4 IN FIRST MAIN TABLE 

cd $ROOT
cd $OUTPUTDIR 
global PLOTOPTS "title("") legend(order(1 "Peace" 2 "War")) plot1opts(lcolor(black) lpattern(dash)) plot2opts(lcolor(black))  ci1opts(color("102 205 0%50")) ci2opts(color(red%80)) graphregion(color(white)) bgcolor(white)" 
global X1 "inc1##c.llgrowthwara inc1##c.llgrowthpeacea inc1##c.llshrinkwarb inc1##c.llshrinkpeaceb"  // version for plots
global CONTROLS "lldist lneigh lcoastala lcoastalb llagea llageb llnumneighsa llnumneighsb  llelevsda llelevsdb" 
global RANGE "(0(1)14)"
global PARAMS " lldist=5.3 lneigh=1 llnumneighsa=2 llnumneighsb=2 llareab==6.3 llelevsda=3.3 llelevsdb=3.3 llagea=4.5 llageb=4.5 lcoastala=1 lcoastalb=0 "

// GENERATE FIGURE 11
reghdfe lnga0 inc1##c.llr llareab $CONTROLS, absorb(year) cluster(idd2 ida idb)
 
margins inc1, at (llr = (0.1(0.1)1)  $PARAMS)
marginsplot, xlab(0.1(0.1)1) recast(line) recastci(rarea) $PLOTOPTS
graph export dyads_llr.png, replace 




reghdfe lnga0 $X1 llareaa llareab $CONTROLS, absorb(year) cluster(idd2 ida idb)

// GENERATE FIGURE 12a
margins inc1, at (llgrowthwara = $RANGE llareaa=6.2 llareab=6.2 $PARAMS)
marginsplot, xlab$RANGE recast(line) recastci(rarea) $PLOTOPTS
graph export dyads_llgrowthwara.png, replace 


// GENERATE FIGURE 12b
margins inc1, at (llshrinkwarb = $RANGE llareaa=6.2 llareaa=6.2 $PARAMS)
marginsplot, xlab$RANGE recast(line) recastci(rarea) $PLOTOPTS
graph export dyads_llshrinkwarb.png, replace 



// APPENDIX: INTERACTION PLOTS WITH DYAD FE

reghdfe lnga0 $X1 llareaa llareab $CONTROLS, absorb(year idd) cluster(idd2 ida idb)

// GENERATE FIGURE A8a 
margins inc1, at (llgrowthwara = $RANGE llareaa=6.2 $PARAMS)
marginsplot, xlab$RANGE recast(line) recastci(rarea)  $PLOTOPTS
graph export dyads_llgrowthwaraFE.png, replace 

// GENERATE FIGURE A8b
margins inc1, at (llshrinkwarb = $RANGE llareaa=6.2 $PARAMS)
marginsplot, xlab$RANGE recast(line) recastci(rarea) $PLOTOPTS
graph export dyads_llshrinkwarbFE.png, replace 


// OTHER INTERACTION PLOTS WITH PEACEFUL EFFECTS NOT SHOWN IN PAPER OR IN APPENDIX

reghdfe lnga0 $X1 llareaa llareab $CONTROLS, absorb(year) cluster(idd2 ida idb)


margins inc1, at (llshrinkpeaceb = $RANGE llareaa=6.2 $PARAMS)
marginsplot, xlab$RANGE recast(line) recastci(rarea) $PLOTOPTS
graph export dyads_llshrinkoeaceb.png, replace 

margins inc1, at (llgrowthpeacea = $RANGE llareaa=6.2 $PARAMS)
marginsplot, xlab$RANGE recast(line) recastci(rarea) $PLOTOPTS
graph export dyads_llgrowthpeacea.png, replace 


reghdfe lnga0 $X1 llareaa llareab $CONTROLS, absorb(year idd) cluster(idd2 ida idb)

margins inc1, at (llgrowthpeacea = $RANGE llareaa=6.2 $PARAMS)
marginsplot, xlab$RANGE recast(line) recastci(rarea) $PLOTOPTS
graph export dyads_llgrowthpeaceaFE.png, replace 


margins inc1, at (llshrinkpeaceb = $RANGE llareaa=6.2 $PARAMS)
marginsplot, xlab$RANGE recast(line) recastci(rarea) $PLOTOPTS
graph export dyads_llshrinkoeacebFE.png, replace 





